package com.ctgu.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctgu.server.dao.dto.GroupRoleDto;
import com.ctgu.server.dao.entity.GroupRole;
import com.ctgu.server.dao.vo.Result;
import com.ctgu.server.service.GroupRoleService;
import com.ctgu.server.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.validation.constraints.NotEmpty;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ctgu
 * @since 2023-05-30
 */
@RestController
@RequestMapping("/server/groupRole")
@CrossOrigin
@Api(tags = "组织操作接口" )
public class GroupRoleController {
    @Autowired
    private GroupRoleService groupRoleService;
    //添加身份组
    @PostMapping("/insert")
    @ApiOperation(notes = "新增身份组信息",value = "insertGroupRole")
    public Result insertGroupRole( @RequestBody GroupRoleDto groupRoleDto){

        return groupRoleService.insertGroupRole(groupRoleDto.getGroupId(),groupRoleDto.getIdentify());
    }
    //修改身份组
    @PostMapping("/update")
    @ApiOperation(notes = "修改身份信息",value = "updateGroupUser")
    public Result updateGroupUser(@RequestBody GroupRoleDto groupRoleDto){

        return groupRoleService.updateGroupRole(groupRoleDto);
    }

    //删除身份组
    @DeleteMapping("/delete")
    @ApiOperation(notes = "删除身份组信息",value = "deleteGroupRole")
    public Result deleteGroupRole( @RequestBody GroupRoleDto groupRoleDto){
        //判断用户身份
        if(groupRoleDto.getId()==null||groupRoleDto.getUserId()==null){
            return ResultUtil.error("Id为空,用户id为空");
        }
        groupRoleService.removeById(groupRoleDto.getId());
       return ResultUtil.success("成功删除身份组信息");
    }

    //获取身份组信息
    @GetMapping("/list")
    @ApiOperation(notes = "查询身份组信息",value = "listGroupRole")
    public Result listGroupRole( GroupRoleDto groupRoleDto){
        if(groupRoleDto.getGroupId()==null){
            return ResultUtil.success("参数为空");
        }
        LambdaQueryWrapper<GroupRole> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GroupRole::getGroupId,groupRoleDto.getGroupId());
        return ResultUtil.success(groupRoleService.list(lambdaQueryWrapper));
    }
}

